<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" @click="push">推送</el-button>
    </div>

    <!--表格开始-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" width="120px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="推送时间" width="250px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ renderTime(scope.row.createdAt) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="500px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="delRoles(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      title="新增推送"
      :visible.sync="notices"
      width="40%"
      :before-close="Close"
    >
      <el-form ref="form" label-width="80px">
        <MDinput v-model="content" :maxlength="100" type="text" name="name" style="margin-top: 10px;width: 80%;margin-left: 50px;">
          内容
        </MDinput>
        <span v-show="contentShortLength" class="word-counter" style="float: right;margin-right: auto;color: red;margin-top: -14px;">{{ contentShortLength }}字</span>
        <el-form-item style="margin-top: 50px;">
          <el-button type="primary" @click="onAdd">保存</el-button>
          <el-button @click="roles = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import MDinput from '@/components/MDinput'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getPush, Push, deletePush } from '@/api/push'
import { renderTime } from '../../utils'
export default {
  name: 'Notices',
  components: { Pagination, MDinput },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      notices: false,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        keywords: undefined
      },
      content: ''
    }
  },
  computed: {
    contentShortLength() {
      return this.content.length
    }
  },
  created() {
    this.getList()
  },
  methods: {
    renderTime,
    // 获取数据
    getList() {
      this.listLoading = true
      getPush(this.listQuery).then(res => {
        const { data: { count, rows }} = res.data
        this.total = count
        this.list = rows
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 删除
    delRoles(id) {
      this.$confirm('此操作将删除该通知, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePush(id)
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.getList()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    Close() {
      this.notices = false
    },
    // push
    push() {
      this.notices = true
    },
    // onAdd
    onAdd() {
      const data = { content: this.content }
      Push(data).then(res => {
        const { msg } = res.data
        this.$message({
          type: 'success',
          message: msg
        })
        this.notices = false
        this.getList()
      }).catch(() => {})
    }
  }
}
</script>
<style></style>
